package com.hzhr.iot.login.controller;

import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrIotUtil;
import com.hzhr.iot.login.controller.base.BaseController;
import com.hzhr.iot.login.model.token.TokenModel;
import com.hzhr.iot.login.service.TokenService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

@Api(tags = "token", description = "令牌相关服务")
@RestController
@RequestMapping(value = "")
public class TokenController extends BaseController {
    @Autowired
    TokenService tokenService;

    //region /tokens GET
    //region swagger响应码 : get
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 403, message = "----异常代码 : 201  \n----异常类型 : 业务流程异常  \n----异常业务 : 认证用户  \n----异常说明 : 用户不存在或密码不匹配  \n\n", response = ResponseExceptionDto.class),
    })
    //endregion
    //region swagger说明 : get
    @ApiOperation(value="获取用户的登录令牌", notes="获取用户的登录令牌  \n简介 : 获取用户的登录令牌  \n步骤 :  \n1. 认证用户  \n2. 创建令牌  \n3. 如果用户存在当前卡片，直接返回；否则，创建新用户卡片并更新用户当前令牌  \n4. 如果用户没有rsToken，则创建rsToken并返回；否则，直接返回  \n5. 返回创建的令牌  \n",produces = "application/json")
    //endregion
    //region swagger参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "password", value = "密码",required = true, dataType = "string", paramType = "header"),
    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/tokens", method = RequestMethod.GET)
    //endregion
    public ResponseEntity<TokenModel> get(@RequestParam @ApiIgnore Map<String, Object> query) throws ServiceException {
        Map<String, String> header = initHeader();
        header = HzhrIotUtil.initGlobalHeader(header);

        TokenModel responseBody = tokenService.get(header, query);
        return new ResponseEntity<>(responseBody, HttpStatus.OK);
        //endregion
    }
    //endregion

}































































































































































































